var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require('cors');
var fileUpload = require('express-fileupload');
const openAI = require('./utils/openAI');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var authRouter = require('./routes/auth');
var customerInfoRouter = require('./routes/customerInfo');
var schedulerRouter = require('./routes/scheduler');
var churn_record = require('./routes/churn_record');
var project_category = require('./routes/project_category');
var project = require('./routes/project');
var appointment = require('./routes/appointment');
var consumption = require('./routes/consumption');
var department = require('./routes/department');
var position = require('./routes/position');
var employee = require('./routes/employee');
var role = require('./routes/role');
var user = require('./routes/user');
var revisit_task = require('./routes/revisit_task');
var revisit_record = require('./routes/revisit_record');
var business_opportunity = require('./routes/business_opportunity');
var follow_record = require('./routes/follow_record');
var user_ctrl_log = require('./routes/user_ctrl_log');
var app = express();

app.use(fileUpload({
  limits: { fileSize: 10 * 1024 * 1024 }, // 10MB 限制
  useTempFiles: true,
  tempFileDir: '/tmp/'
}));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// 启用CORS
app.use(cors());

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/auth', authRouter);
app.use('/customerInfo', customerInfoRouter);
app.use('/scheduler', schedulerRouter);
app.use('/churn_record', churn_record);
app.use('/project_category', project_category);
app.use('/project', project);
app.use('/appointment', appointment);
app.use('/consumption', consumption);
app.use('/department', department);
app.use('/position', position);
app.use('/employee', employee);
app.use('/role', role);
app.use('/user', user);
app.use('/revisit_task', revisit_task);
app.use('/revisit_record', revisit_record);
app.use('/business_opportunity', business_opportunity);
app.use('/follow_record', follow_record);
app.use('/user_ctrl_log', user_ctrl_log);
// 启动定时任务系统
const taskScheduler = require('./utils/scheduler');
// taskScheduler.start();

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
